<?php $this->load->view('partial/head'); ?>

<h2><?= $mode . " PAKET MAKANAN" ?></h2>

<form method="post" action="<?= base_url()."index.php/koki/save_paket" ?>">
	<input type="hidden" name="mode" value="<?= $mode ?>" />
	<?php if($mode == 'EDIT'){ ?>
		<input type="hidden" name="id_paket" value="<?= $detail['id_paket'] ?>" />
	<?php } ?>
	
	<table cellpadding="10">
		<tr>
			<td>Nama</td>
			<td><input name="nama" size="50" value="<?=($mode == 'EDIT' ? $detail['nama'] : '')?>" /></td>
		</tr>
		<tr>
			<td>Harga</td>
			<td>Rp. <input name="harga" size="50" value="<?=($mode == 'EDIT' ? $detail['harga'] : '')?>" /></td>
		</tr>
		<tr>
			<td>Deskripsi <br/>(<i>Isi dari paket makanan, <br/>cth: nasi + ikan goreng + teh manis</i>)</td>
			<td>
				<textarea name="deskripsi" cols="50" rows="5"><?=($mode == 'EDIT' ? $detail['deskripsi'] : '')?></textarea>
			</td>
		</tr>
		<tr>
			<td>Bahan-bahan</td>
			<td><a id="add-bahan-baku" href="#">(+) Tambah Bahan Baku</a><br/>
				<?php if($mode == 'ADD'){ ?>
					<select name="bahan_baku[]" class="bahan_baku">
						<option value="">- Pilih Bahan Baku -</option>
						<?php foreach($bahan as $bahan){ ?>
							<option value="<?=$bahan['id_bahan_baku']?>"><?=$bahan['nama']?></option>
						<?php } ?>
					</select>
					sebanyak : <input type="text" name="ukuran[]" size="3" /> kg</br>
				<?php }else{ 
					foreach($bahan_paket as $bp){ ?>
						<select name="bahan_baku[]" class="bahan_baku">
							<option value="">- Pilih Bahan Baku -</option>
							<?php foreach($bahan as $b){ ?>
								<option value="<?=$b['id_bahan_baku']?>" <?=($b['id_bahan_baku'] == $bp['id_bahan_baku'] ? 'selected' : '')?>>
									<?=$b['nama']?>
								</option>
							<?php } ?>
						</select>
						sebanyak : <input type="text" name="ukuran[]" size="3" value="<?=$bp['ukuran']?>" /> kg</br>
				<?php } 
				} ?>
				<div id="bahan-wrap"></div>
			</td>
		</tr>
		<tr>
			<td></td>
			<td>
				<a href="<?= base_url()."index.php/koki/paket_makanan" ?>"><input type="button" value="Kembali" /></a>
				<input type="submit" value="Simpan" />
			</td>
		</tr>
	</table>
</form>

<script>
$(function(){
	var jumlah_bahan = $('select.bahan_baku option').length - 1;
	$('#add-bahan-baku').click(function(e){
		e.preventDefault();
		if($('select.bahan_baku').length < jumlah_bahan){
			var bahan = '<select name="bahan_baku[]" class="bahan_baku"><option value="">- Pilih Bahan Baku -</option>';
			var last_checked = $('select.bahan_baku').last().val();
			$('select.bahan_baku:last option:not(:first)').each(function(){	
				if($(this).val() != last_checked){
					bahan += '<option value="'+ $(this).val() +'">' + $(this).text() + '</option>';
				}
			});
			bahan += '</select>';
			bahan += ' sebanyak : <input type="text" name="ukuran[]" size="3" /> kg</br>';
			$('#bahan-wrap').append(bahan);
		}else{
			$('#add-bahan-baku').after('<font color="red">Tidak ada bahan baku lain yang tersedia</font>');			
			$('#add-bahan-baku').hide();
		}
	});
});
</script>